package com.qk.mangement.aop;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.qk.vo.portal.PortalVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Date:2025/10/17
 * Author:zxc
 * Description
 */
@Slf4j
@Component
@Aspect
public class RedisAspect {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String CACHE_PORTAL_KEY_PORTAL = "qk:portal:";

    @Around("@annotation(com.qk.mangement.annoation.RedisAnnotation)")
    public Object redisAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        String redisKey = CACHE_PORTAL_KEY_PORTAL + "reportOverview";
        String jsonStr = null;
        try {
            jsonStr = redisTemplate.opsForValue().get(redisKey);
            if (ObjectUtil.isNotEmpty(jsonStr)) {
                log.info("从缓存中获取数据");
                PortalVO vo = JSONUtil.toBean(jsonStr, PortalVO.class);
                return vo;
            }
        } catch (Exception e) {
            log.error("查询缓存异常", e);
        }
        Object result = joinPoint.proceed();
        try {
            String json = JSONUtil.toJsonStr(result);
            redisTemplate.opsForValue().set(redisKey, json);
        } catch (Exception e) {
            log.error("添加缓存异常", e);
        }
        return result;

    }
}